package com.rojored.controls.controlBarClasses 
{

import com.rojored.controls.progressDisplayClasses.BaseProgressDisplay;
import com.rojored.controls.progressDisplayClasses.IPlayheadProgressDisplay;
import flash.events.Event;

/**
 *  Display for media playhead progress.
 *
 *  @author gabriel montagné láscaris-comneno gabriel@rojored.com
 */
public class PlayheadProgressDisplay extends BaseProgressDisplay 
    implements IPlayheadProgressDisplay
{

    //--------------------------------------------------------------------------
    //
    //  Constructor
    //
    //--------------------------------------------------------------------------

    /**
     *  Constructor
     */
    public function PlayheadProgressDisplay() 
    {
        super();
        progressValues = Vector.<Number>([0]);
    }

    //--------------------------------------------------------------------------
    //
    //  Properties: IPlayheadProgressDisplay
    //
    //--------------------------------------------------------------------------
    
    //--------------------------------------
    // playheadTime 
    //--------------------------------------

    /**
     *  @private
     *  Storage for the playheadTime property
     */
    private var _playheadTime:Number = 0;
    
    /**
     *  @private
     */
    private var playheadTimeChanged:Boolean = true;

    [Bindable("playheadTimeChanged")]

    /**
     *  @copy com.rojored.controls.progressDisplayClasses.IPlayheadProgressDisplay#playheadTime
     */
    public function get playheadTime():Number
    {
        return _playheadTime;
    }

    /**
     *  @private
     */
    public function set playheadTime(value:Number):void
    {
        if (_playheadTime == value)
            return;

        _playheadTime = value;
        playheadTimeChanged = true;
        invalidateProperties();
        dispatchEvent(new Event("playheadTimeChanged"));
    }
    
    //--------------------------------------
    // totalTime 
    //--------------------------------------

    /**
     *  @private
     *  Storage for the totalTime property
     */
    private var _totalTime:Number = 1;
    
    /**
     *  @private
     */
    private var totalTimeChanged:Boolean = true;

    [Bindable("totalTimeChanged")]

    /**
     *  @copy com.rojored.controls.progressDisplayClasses.IPlayheadProgressDisplay#totalTime
     */
    public function get totalTime():Number
    {
        return _totalTime;
    }

    /**
     *  @private
     */
    public function set totalTime(value:Number):void
    {
        if (_totalTime == value)
            return;

        _totalTime = value;
        totalTimeChanged = true;
        invalidateProperties();
        dispatchEvent(new Event("totalTimeChanged"));
    }

    //--------------------------------------------------------------------------
    //
    //  Overriden methods: UIComponent
    //
    //--------------------------------------------------------------------------
    
    /**
     *  @private
     */
    override protected function commitProperties():void
    {
        super.commitProperties();
        if (playheadTimeChanged || totalTimeChanged)
        {
            playheadTimeChanged = false; 
            totalTimeChanged = false;
            var playheadFraction:Number = 
                totalTime ? playheadTime / totalTime : 0;

            if (progressValues[0] != playheadFraction)
            {
                progressValues[0] = playheadFraction
                invalidateDisplayList();
            }
        }
    }
}
}
